/*
 * @(#)PalindromeArray.java	1.0 Apr 26, 2008
 *
 *	The MIT License
 *
 *	Copyright (c) 2008 Malachi de AElfweald <malachid@gmail.com>
 *
 *	Permission is hereby granted, free of charge, to any person obtaining a copy
 *	of this software and associated documentation files (the "Software"), to deal
 *	in the Software without restriction, including without limitation the rights
 *	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *	copies of the Software, and to permit persons to whom the Software is
 *	furnished to do so, subject to the following conditions:
 *
 *	The above copyright notice and this permission notice shall be included in
 *	all copies or substantial portions of the Software.
 *
 *	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *	THE SOFTWARE.
 */
package org.eoti.math;

import java.math.BigInteger;
import java.util.*;

/**
 * This needs to be reimplemented to not be bound to Integer.MAX_VALUE, which currently
 * Java arrays[], ArrayList, etc all are...
 *
 * probably use some kind of binary tree or something that doesn't require an explicit
 * get(int)/set(int)
 */
public class PalidromeArray
	implements Iterable<BigInteger>
{
	protected static BigInteger TWO = BigInteger.valueOf(2);
	protected BigInteger totalLength;
	protected BigInteger[] array;
	protected boolean isEven = true;

	public PalidromeArray(PalidromeArray array)
	{
		this.totalLength = array.totalLength;
		this.array = array.array;
		this.isEven = array.isEven;
	}

	public PalidromeArray(BigInteger[] array)
	{
		this(BigInteger.valueOf(array.length));
		int halfLength = this.halfLength().intValue();
		for(int i=0; i<halfLength; i++)
			this.array[i] = array[i];
	}

	public PalidromeArray(BigInteger totalLength)
	{
		this.totalLength = totalLength;
		int halfLength = totalLength.intValue()/2;
		if(MathUtil.isOdd(totalLength))
		{
			isEven = false;
			halfLength++;
		}
		array = new BigInteger[halfLength];
	}

	public BigInteger halfLength(){return BigInteger.valueOf(array.length);}
	public BigInteger totalLength(){return totalLength;}
	public BigInteger get(int position){return array[getPosition(position)];}
	public void set(int position, BigInteger value){array[getPosition(position)] = value;}

	protected int getPosition(int position)
	{
		/**
		 * {1,4,6,4,1} would have {1,4,6} in our array
		 * array.length = 3
		 * 0: return 1
		 * 1: return 4
		 * 2: return 6
		 *
		 * {1,3,3,1} would have {1,3} in our array
		 * array.length = 2
		 * 0: return 1
		 * 1: return 3
		 */
		if(position < array.length)
			return position;

		/**
		 * {1,4,6,4,1} would have {1,4,6} in our array
		 * totalLength = 5
		 * 3: return 4
		 * 4: return 1
		 *
		 * {1,3,3,1} would have {1,3} in our array
		 * totalLength = 4
		 * 2: return 3
		 * 3: return 1
		 */
		return totalLength.intValue()-position-1;
	}

	public Iterator<BigInteger> iterator()
	{
		return new PalidromeArrayIterator(this);
	}

	class PalidromeArrayIterator
	implements Iterator<BigInteger>
	{
	    protected PalidromeArray array;
		protected int position = 0;

		public PalidromeArrayIterator(PalidromeArray array)
		{
		    this.array = array;
		}

		public boolean hasNext()
		{
			return position<array.totalLength.intValue();
		}

		public BigInteger next()
		{
			return array.get(position++);
		}

		public void remove()
		{
			throw new UnsupportedOperationException("Not supported");
		}
	}
}
